package com.ts.main.admin.controller;

import com.aliyuncs.exceptions.ClientException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ts.common.dto.FileDto;
import com.ts.main.admin.exception.R;
import com.ts.main.admin.service.FileServiceImpl;
import com.ts.main.admin.service.OssFileServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 小程序-文件管理
 * @Author:lk
 * @Date: 2025/5/18  02:43
 */
@AllArgsConstructor
@RestController
@RequestMapping("/mini/file")
@Tag(name = "文件管理")
public class FileController {

    private final FileServiceImpl fileService;
    private final OssFileServiceImpl ossFileService;

    @GetMapping("/ossSecurityToken")
    @Operation(summary = "获取OSS上传文件的-临时认证")
    @ApiOperationSupport(order = 1)
    public R ossSecurityToken() throws ClientException {
        return ossFileService.ossSecurityToken();
    }

    @GetMapping("/getOssFileToken")
    @Operation(summary = "获取OSS上传文件的-上传令牌")
    @ApiOperationSupport(order = 2)
    public R getOssFileToken() {
        return ossFileService.getOssFileToken();
    }

    @PostMapping("/ossUploadFile")
    @Operation(summary = "上传文件(oss服务器)")
    @ApiOperationSupport(order = 3)
    public R ossUploadFile(@Schema(description = "文件") @RequestPart("file") MultipartFile file) {
        return R.ok(ossFileService.ossUploadFile(file));
    }

    @PostMapping("/ossUploadFileList")
    @Operation(summary = "批量上传文件(oss服务器)")
    @ApiOperationSupport(order = 4)
    public R ossUploadFileList(@Schema(description = "文件") @RequestPart("files") MultipartFile[] files) {
        return R.ok(ossFileService.ossUploadFileList(files));
    }

    @PostMapping("/addGoodsFile")
    @Operation(summary = "添加单个文件到数据库")
    @ApiOperationSupport(order = 5)
    public R<FileDto> addGoodsFile(@RequestBody FileDto dto) {
        return R.ok(fileService.addGoodsFile(dto));
    }

    @PostMapping("/addGoodsListFile")
    @Operation(summary = "添加文件集合到数据库")
    @ApiOperationSupport(order = 6)
    public R<List<FileDto>> addGoodsListFile(@RequestBody List<FileDto> dtos) {
        return R.ok(fileService.addGoodsListFile(dtos));
    }

}
